home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 192_01 / sgrep.c < prev    next >
Text File  |  1980-01-01  |  21KB  |  787 lines

  1. /*
  2.  *
  3.  * The  information  in  this  document  is  subject  to  change
  4.  * without  notice  and  should not be construed as a commitment
  5.  * by Digital Equipment Corporation or by DECUS.
  6.  *
  7.  * Neither Digital Equipment Corporation, DECUS, nor the authors
  8.  * assume any responsibility for the use or reliability of  this
  9.  * document or the described software.
  10.  *
  11.  *      Copyright (C) 1980, DECUS
  12.  *
  13.  * General permission to copy or modify, but not for profit,  is
  14.  * hereby  granted,  provided that the above copyright notice is
  15.  * included and reference made to  the  fact  that  reproduction
  16.  * privileges were granted by DECUS.
  17.  *
  18.  */
  19.  
  20. #include "stdio.h"
  21.  
  22. /*
  23.  * grep.
  24.  *
  25.  * Runs on the Decus compiler or on vms.
  26.  * Converted for BDS compiler (under CP/M-80), 20-Jan-83, by Chris Kern.
  27.  * Converted to IBM PC with CI-C86 C Compiler June 1983 by David N. Smith
  28.  * On vms, define as:
  29.  *      grep :== "$disk:[account]grep"     (native)
  30.  *      grep :== "$disk:[account]grep grep"     (Decus)
  31.  *
  32.  * sgrep.
  33.  *
  34.  * Addition of string substitution capability,
  35.  * multiple pattern search, upper-lower case option, scanning options,
  36.  * and name changed to sgrep, April 1986 by James J. McKeon.
  37.  * Runs on IBM PC or compatibles using ECO C88 compiler.
  38.  *
  39.  * For help type "sgrep ?".  See below for more information.
  40.  *
  41.  */
  42.  
  43.  
  44. char    *documentation[] = {
  45. "Sgrep searches a file for a given pattern and substitutes a pattern.",
  46. "If no substitution is required, the command sgrep -myn corresponds",
  47. "to grep -n and will match only. Output is to the screen and may be",
  48. "re-directed using \">\" at the DOS level. Execute by",
  49. "  sgrep [flags] pattern-file input-file",
  50. "",
  51. "Flags are single characters preceeded by '-':",
  52. "   -c      Only a count of matching lines is printed",
  53. "   -m      Match patterns only, no substitutions.",
  54. "   -n      Each line is preceeded by its line number",
  55. "   -v      Only print non-matching lines",
  56. "   -y      Upper and lower case match.",
  57. "",
  58. 0 };
  59.  
  60. char    *patdoc[] = {
  61. "Each match pattern is on a separate line followed by its substitute",
  62. "pattern, if any, also on a separate line.",
  63.  
  64. " MATCH PATTERNS",
  65. "The regular_expression defines the pattern to search for.  Upper and",
  66. "lower-case are regarded as different unless -y option is used.",
  67. "x      An ordinary character (not mentioned below) matches that character.",
  68. "'\\'    The backslash quotes any character.  \"\\$\" matches a dollar-sign.",
  69. "'$'    Matches beginning or end of line.",
  70. "'.'    A period matches any character except \"new-line\".",
  71. "':a'   A colon matches a class of characters described by the following",
  72. "':d'     character.  \":a\" matches any alphabetic, \":d\" matches digits,",
  73. "':n'     \":n\" matches alphanumerics, \": \" matches spaces, tabs, and",
  74. "': '     other control characters except new-line.",
  75. "'*'    An expression followed by an asterisk matches zero or more",
  76. "       occurrances of that expression: \"fo*\" matches \"f\", \"fo\"",
  77. "       \"foo\", etc.",
  78. "'+'    An expression followed by a plus sign matches one or more",
  79. "       occurrances of that expression: \"fo+\" matches \"fo\", etc.",
  80. "'-'    An expression followed by a minus sign optionally matches",
  81. "       the expression.",
  82. "'[]'   A string enclosed in square brackets matches any character in",
  83. "       that string, but no others.  If the first character in the",
  84. "       string is a circumflex, the expression matches any character",
  85. "       except \"new-line\" and the characters in the string.  For",
  86. "       example, \"[xyz]\" matches \"xx\" and \"zyx\", while \"[^xyz]\"",
  87. "       matches \"abc\" but not \"axb\".  A range of characters may be",
  88. "       specified by two characters separated by \"-\".  Note that,",
  89. "       [a-z] matches alphabetics, while [z-a] never matches.",
  90. "The concatenation of regular expressions is a regular expression.",
  91.  
  92. " Scanning options:",
  93. "The default scanning option is match all occurences of the pattern.",
  94. "'@'    at the beginning of the pattern, is equivalent to",
  95. "       \"$: *\", meaning match first non-whitespace pattern.",
  96. "'@e'   at the end of a pattern means that if a match is",
  97. "       found (and a substitution made), end all pattern search",
  98. "       on current line.",
  99. "'@r'   at the end of a pattern means that after a match",
  100. "       (and substitution), rescan line until no match occurs.",
  101.  
  102. " SUBSTITUTE PATTERNS ",
  103. "The only control character for substitute patterns is \"?\".",
  104. "Any other character represents itself. Only the characters ? and",
  105. " \\ itself need to be quoted.",
  106.  
  107. "'?n'   where n is a non-zero digit, indicates the position where",
  108. "       the nth wildcard string is to be placed.",
  109. "A wildcard string is any string which is not completely fixed,",
  110. "both as to the characters and number of characters in the string.",
  111. 0};
  112.  
  113. #define LMAX    100
  114. #define PMAX    2000
  115. #define NPATMAX 100
  116.  
  117. #define CHR     1
  118. #define SCOP    2
  119. #define BEOL    3
  120. #define ANY     4
  121. #define CLASS   5
  122. #define NCLASS  6
  123. #define STAR    7
  124. #define PLUS    8
  125. #define MINUS   9
  126. #define ALPHA   10
  127. #define DIGIT   11
  128. #define NALPHA  12
  129. #define PUNCT   13
  130. #define RANGE   14
  131. #define ENDPAT  15
  132. #define lowopt(x) yflag?tolower(x):x
  133.  
  134. int     cflag;
  135. int     mflag;
  136. int     nflag;
  137. int     vflag;
  138. int     yflag;
  139.  
  140. int     debug   =       0;         /* Set for debug code      */
  141.  
  142. char    *pp, *psp;
  143.  
  144. #ifndef vms
  145. char    file_name[81];
  146. #endif
  147.  
  148. char    lbuf[LMAX], slbuf[LMAX];
  149. char    pbuf[PMAX], spbuf[PMAX];
  150. char    * wldstr[19][2];  /* Location of wildcard strings */
  151. int     nws, npat, rescan, nxtln;
  152. char   *mpp[NPATMAX], *mpsp[NPATMAX];  /* Location of patterns */
  153. /*******************************************************/
  154.  
  155. main(argc, argv)
  156. char *argv[];
  157. {
  158.    register char   *p;
  159.    register int    c, i;
  160.  
  161.    FILE            *f;
  162.  
  163.    if (argc <= 1)
  164.       usage("No arguments");
  165.    if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
  166.       help(documentation);
  167.       help(patdoc);
  168.       return;
  169.       }
  170.       p=argv[argc-1];
  171.       if ((f=fopen(p, "r")) == NULL) cant(p);
  172.       p = argv[1];
  173.       if (*p == '-') {
  174.          ++p;
  175.          while (c = *p++) {
  176.             switch(tolower(c)) {
  177.  
  178.             case 'c':
  179.                ++cflag;
  180.                break;
  181.  
  182.             case 'd':
  183.                ++debug;
  184.                break;
  185.  
  186.             case 'm':
  187.                ++mflag;
  188.                break;
  189.  
  190.             case 'n':
  191.                ++nflag;
  192.                break;
  193.  
  194.             case 'v':
  195.                ++vflag;
  196.                break;
  197.  
  198.             case 'y':
  199.                ++yflag;
  200.                break;
  201.  
  202.             default:
  203.                usage("Unknown flag");
  204.             }
  205.          }
  206.         p=argv[2];
  207.       }
  208.       compat(p);
  209.       grep(f, p);
  210.       fclose(f);
  211. }
  212.  
  213. /*******************************************************/
  214.  
  215. cant(s)
  216. char *s;
  217. {
  218.    fprintf(stderr, "%s: cannot open\n", s);
  219. }
  220.  
  221.  
  222. /*******************************************************/
  223.  
  224. help(hp)
  225. char **hp;  /* dns added extra '*'  */
  226. /*
  227.  * Give good help
  228.  */
  229. {
  230.    register char   **dp;
  231.  
  232.    for (dp = hp; *dp; dp++)
  233.       printf("%s\n", *dp);
  234. }
  235.  
  236.  
  237. /*******************************************************/
  238.  
  239. usage(s)
  240. char    *s;
  241. {
  242.    fprintf(stderr, "?SGREP-%s\n", s);
  243.    fprintf(stderr,
  244.   "Usage: sgrep [-mycnv] pattern-file input-file. sgrep ? for help\n");
  245.    exit(1);
  246. }
  247.  
  248.  
  249.  
  250. /*******************************************************/
  251.  
  252.  
  253. compile(source)
  254. char       *source;   /* Pattern to compile         */
  255. /*
  256.  * Compile the pattern into global pbuf[]
  257.  */
  258. {
  259.    register char  *s;         /* Source string pointer     */
  260.    register char  *lp;        /* Last pattern pointer      */
  261.    register int   c;          /* Current character         */
  262.    int            o;          /* Temp                      */
  263.    char           *spp;       /* Save beginning of pattern */
  264.    char           *cclass();  /* Compile class routine     */
  265.  
  266.    s = source;
  267.    if (debug)
  268.       printf("Pattern = %s\n", s);
  269.    while (c = *s++) {
  270.       /*
  271.